package com.sso.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.sso.service.UserService;
import com.sso.utils.Constants;
import com.sso.utils.RedisUtil;
import com.sso.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;

/**
 * @author : luyifei
 * @date : 2021/1/17
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean verifyToken(String token) {
        if (TokenUtil.verify(token)) {
            Map<String, Claim> claims = TokenUtil.getClaims(token);
            String tokenKey = claims.get("userName").asString();
            String tokenCache = getToken(tokenKey);
            if (tokenCache.equals(token)) {
                return true;
            }
        }
        return false;
    }


    @Override
    public void delayToken(String token) {
        Map<String, Claim> claims = TokenUtil.getClaims(token);
        String tokenKey = claims.get("userName").asString();
        redisUtil.set(tokenKey, token, Constants.TOKEN_TTL);
    }

    @Override
    public void deleteToken(String token, HttpServletResponse response) {
        deleteCacheToken(token);
        deleteCookieToken(token, response);
    }

    @Override
    public void deleteCacheToken(String token) {
        Map<String, Claim> claims = TokenUtil.getClaims(token);
        String tokenKey = claims.get("userName").asString();
        // 删除redis中token
        redisUtil.del(tokenKey);
    }

    @Override
    public void deleteCookieToken(String token, HttpServletResponse response) {
        // 删除cookie中token
        Cookie cookie = new Cookie(Constants.TOKEN_COOKIE_KEY, null);
        cookie.setMaxAge(Constants.COOKIE_AGE_ZERO);
        cookie.setPath(Constants.COOKIE_PATH);
        response.addCookie(cookie);
    }

    @Override
    public void saveToken(String token, HttpServletResponse response) {
        Cookie clientCookie = new Cookie(Constants.TOKEN_COOKIE_KEY, token);
        clientCookie.setMaxAge(Constants.COOKIE_AGE_NEGATIVE);
        clientCookie.setPath(Constants.COOKIE_PATH);
        response.addCookie(clientCookie);
        delayToken(token);
    }

    @Override
    public String getCookies(String key, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies || cookies.length < 0) {
            return null;
        }
        String tokenVal = "";
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if (!StringUtils.isEmpty(name) && key.equals(name)) {
                tokenVal = cookie.getValue();
            }
        }

        return tokenVal;
    }

    /**
     * 验证用户登录信息
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public boolean verifyUser(String userName, String password) {
        if (userName.equals(password)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 从redis获取token
     *
     * @param key
     * @return
     */
    public String getToken(String key) {
        Object obj = redisUtil.get(key);
        if (Objects.nonNull(obj)) {
            return obj.toString();
        }
        return "";
    }
}
